home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11073 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  74 lines

  1. Path: news.rain.org!usenet
  2. From: "Guus Leeuw jr." <guusl@eiffel.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: help: templates + destructors
  5. Date: Tue, 12 Mar 1996 08:33:28 -0800
  6. Organization: Interactive Software Engineering Inc. http://www.eiffel.com/
  7. Message-ID: <3145A758.EDB77B0@eiffel.com>
  8. References: <klEd_y200iWTM=AEsS@andrew.cmu.edu>
  9. NNTP-Posting-Host: @outback.eiffel.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.8 i586)
  14.  
  15. Matthew Edward Patton wrote:
  16. [snip snip]
  17. > template<class T>
  18. > class field {
  19. > T value;
  20. > public:
  21. >   field(void) { value = (T) 0; };
  22. >   field(byte) { value = (T) 0; };
  23. >   ~field(void) { return; };
  24. > }
  25. > field<char*>::field(void) {
  26. > // override class default;
  27. > }
  28. > field<char*>::field(byte length) {
  29. > //override class default;
  30. > value = new char[length];
  31. > }
  32. > field<char *>::~field(void) {
  33. > delete[] value;
  34. > }
  35. > The error:
  36. > no 'field' member function declared in class 'field<char*>'
  37. > I took that to mean the compiler can't define a custom destructor if a
  38. > custom constructor doesn't already exist.  Thing is, I've got two
  39. > constructors defined.  I appreciate your help.
  40.  
  41. Nope. You declared your class as being template <class T>. In your
  42. definition you'll have to say:
  43.  
  44. template <class T>
  45. field<T>::field(void) {
  46. ..
  47. }
  48.  
  49. template <class T>
  50. field<T>::field (byte length) {
  51. ..
  52. }
  53.  
  54. template <class T>
  55. field<T>::~field(void) {
  56. ..
  57. }
  58.  
  59. Then, when you want to use it, you say:
  60.  
  61. class A {
  62.     field<char*> _my_field;        // Instatiation of a field
  63.                     // holding a char*
  64. }
  65.  
  66. Hope this helps,
  67.     Guus
  68.